package com.geostar.constructionland.analyse.service;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.config.SpringUtil;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.entity.resulthandler.ResultHandlerCode;
import com.geostar.constructionland.analyse.service.analyse.impl.Analyse;
import com.geostar.constructionland.common.entity.logs.JsydFxrz;
import com.geostar.constructionland.common.global.AnalyseType;
import com.geostar.constructionland.common.service.logs.JsydFxrzService;
import com.geostar.constructionland.common.util.CloneUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author yelll
 * @since 2020-5-27 11:10
 * 分析链表
 **/
@Component
@Slf4j
public class AnalyseChain {
    ObjectMapper objectMapper = new ObjectMapper();

    List<Analyse> analyseChain = new ArrayList<>();

    public void addAnalyse(Analyse analyse) {
        analyseChain.add(analyse);
    }

    // 如果当前没有事务，就新建一个事务，如果已经存在一个事务中，加入到这个事务中（此为主事务）
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public HandlerResult analyse(String ywid) {
        HandlerResult handlerResult = null;
        List<JsydFxrz> fxrzs = new ArrayList<>();
        for (Analyse analyse : analyseChain) {
            /**当前的分析输入*/
            AnalyseModel thisAnalyseModel = analyse.getAnalyseModel();
            JsydFxrz jsydFxrz = getJsydFxrz(thisAnalyseModel);
            try {
                /**占压分析**/
                HttpResult sptAlyResult = analyse.sptAnalyse();
                /**设置分析结果信息**/
                jsydFxrz.setFxjg(jsydFxrz.getFxjg() + ",1");
                handlerResult = analyse.handler(sptAlyResult);

                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                AlyResult alyResult = objectMapper.readValue(sptAlyResult.getBody(), AlyResult.class);

                jsydFxrz.setFxjg(jsydFxrz.getFxjg() + ",1");
                jsydFxrz.setBz(jsydFxrz.getFxjg());
                jsydFxrz.setUniqueTaskId(alyResult.getUniqueTaskId());
                jsydFxrz.setFxjg("true");
            } catch (Exception e) {
                /**异常则设置结果为null**/
                e.printStackTrace();
                jsydFxrz.setFxjg(jsydFxrz.getFxjg() + ",0");
                jsydFxrz.setErrorMsg(e.getMessage());
                jsydFxrz.setBz(jsydFxrz.getFxjg());
                jsydFxrz.setFxjg("false");
                /**添加报错信息*/
                handlerResult = new HandlerResult(ResultHandlerCode.ERROR.getCode(), e.getMessage());
            } finally {
                jsydFxrz.setLogEndTime(new Date());
                fxrzs.add(jsydFxrz);
                if (jsydFxrz.getFxlxMapped().equals(AnalyseType.YPHXFX.getCode())) {
//                    JsydFxrz fxrz_sec = new JsydFxrz();
//                    CloneUtil.copyProperties(jsydFxrz, fxrz_sec);
//                    fxrz_sec.setFxrzid(UUID.randomUUID().toString().replaceAll("-", ""));
//                    fxrz_sec.setFxlx("CDYPHXFX");
//                    fxrzs.add(fxrz_sec);
                } else if (jsydFxrz.getFxlx().equals(AnalyseType.TZHDLFX.getCode())
                        || jsydFxrz.getFxlx().equals(AnalyseType.TZHDLGDFX.getCode())) {

                    JsydFxrz fxrz_sec = new JsydFxrz();
                    CloneUtil.copyProperties(jsydFxrz, fxrz_sec);
                    String TZHGDFX_e = "TZHGDFX" + jsydFxrz.getFxlx().substring(7);
                    fxrz_sec.setFxrzid(UUID.randomUUID().toString().replaceAll("-", ""));
                    fxrz_sec.setFxlx(TZHGDFX_e);
                    fxrzs.add(fxrz_sec);
                    JsydFxrz fxrz_thr = new JsydFxrz();
                    CloneUtil.copyProperties(jsydFxrz, fxrz_thr);
                    fxrz_thr.setFxrzid(UUID.randomUUID().toString().replaceAll("-", ""));
                    String QSFX_e = "QSFX" + jsydFxrz.getFxlx().substring(7);
                    fxrz_thr.setFxlx(QSFX_e);
                    fxrzs.add(fxrz_thr);
                }
            }
            if (handlerResult == null || ResultHandlerCode.ERROR.getCode() == handlerResult.getStatus()) {
                break;
            }
        }
        JsydFxrzService jsydFxrzService = SpringUtil.getBean(JsydFxrzService.class);
        /**保存分析日志*/
        jsydFxrzService.saveJsydFxrz(ywid, fxrzs);
        return handlerResult;
    }

    public JsydFxrz getJsydFxrz(AnalyseModel analyseModel) {
        JsydFxrz jsydFxrz = new JsydFxrz();
        jsydFxrz.setFxbs(analyseModel.getAlyId());
        jsydFxrz.setLogStartTime(new Date());
        jsydFxrz.setFxsj(new Date());
        jsydFxrz.setYwid(analyseModel.getYwid());
        jsydFxrz.setFxlx(analyseModel.getFxlx());
        jsydFxrz.setFxjg("");
        jsydFxrz.setFxlxMapped(analyseModel.getFxlxMapped());
        jsydFxrz.setDlbmbb(analyseModel.getDlbmbbh());
        return jsydFxrz;
    }

}
